<?php

namespace app\controllers\admin\uni\user;

use app\controllers\admin\base\AdminController;
use app\models\AwardResult as Model;

class AwardResultController extends AdminController
{
    public function actionList()
    {
        $user_id = $this->post('user_id');
        $award_sn = $this->post('award_sn');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $query = Model::find()
            ->orderBy(['id' => SORT_DESC]);

        if ($user_id) {
            $query->andWhere(['user_id' => $user_id]);
        }
        if ($award_sn) {
            $query->andWhere(['award_sn' => $award_sn]);
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->with(['award', 'user'])
            ->limit($page_size)
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            if ($item['user'] && $item['user']['nickname']) {
                $item['user']['nickname'] = base64_decode($item['user']['nickname']);
            }
        }

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);
    }

    // public function actionEdit()
    // {
    //     $data = $this->post();
    //
    //     try {
    //         if (isset($data['id']) && $data['id'] > 0) {
    //             $model = Model::findOne(['id' => $data['id']]);
    //             if (!$model) {
    //                 return $this->fail('操作失败1');
    //             }
    //         } else {
    //             $model = new Model();
    //         }
    //         $data = ['AwardList' => $data];
    //         if ($model->load($data) && $model->save()) {
    //             return $this->success($model, '操作成功');
    //         }
    //
    //     } catch (\Exception $e) {
    //         return $this->fail($e->getMessage());
    //     }
    // }
    //
    // public function actionRemove()
    // {
    //     $id = $this->post('id');
    //
    //     try {
    //         $model = Model::findOne(['id' => $id]);
    //         $model->status = -99;
    //         $model->save();
    //         return $this->success($model, '删除成功');
    //     } catch (\Exception $e) {
    //         return $this->fail($e->getMessage());
    //     }
    // }
    //
    // public function actionChangeStatus()
    // {
    //     $id = intval($this->post('id'));
    //     $status = intval($this->post('status'));
    //
    //     try {
    //         $model = Model::findOne(['id' => $id]);
    //         $model->status = $status;
    //         $model->save();
    //
    //         return $this->success();
    //     } catch (\Exception $e) {
    //         return $this->fail('操作错误: ' . $e->getMessage());
    //     }
    // }
    //
    // public function actionBatchRemove()
    // {
    //     $ids = $this->post('ids');
    //
    //     try {
    //         Model::updateAll(['status' => -99], ['id' => $ids]);
    //
    //         return $this->success();
    //     } catch (\Exception $e) {
    //         return $this->fail('删除错误: ' . $e->getMessage());
    //     }
    // }
}